BASHing Through Scripts

Written by Ken Gypen

November 26, 2007 | 08:12

Tags: #script #shell #unix

Companies: #bash

Image Script - Resizing and watermarking

Now, onto the resizing. For this, we’ll use convert as our main weapon of choice. In this case we won’t append our code, but we’ll replace the “mode=portrait” and “mode=landscape” parts with the correct code. Let’s start with the ‘portrait’ mode.

convert -resize "275x365" ${i} small_${i}

We’ll resize the images to "275x365", which preserves the aspect ratio. Again, we use ${i} as our input file. For the output file we’ll prepend the name with small_, which leads to small_BASH.png in our case. For landscape mode we use the same line of code, but just replace "275x365" with "365x275".

So our ‘if’ statement should look like this:
 if [ ${width} -lt ${height} ]
then
convert -resize "275x365" ${i} small_${i}
else
convert -resize "365x275" ${i} small_${i}
fi 

We’re getting close to completion now, only need to add the watermark to both our images. Watermarking is done by merging our image with a semi transparent one, called BASHWatermark.png in this case. This is done through the command composite. So let’s start appending to our file, to add the finishing touch.

composite -gravity southwest -compose src-over BASHWatermark.png ${i} ${i}
composite -gravity southwest -compose src-over BASHWatermark.png small_${i} small_${i}
Now, what does this do? Well, composite merges images, and the ‘gravity’ switch determines the position of the first image that’s merged on top of the second one. In our case, this is the bottom left corner, aka southwest. The ‘compose’ switch is strictly there to circumvent a bug in Debian and Ubuntu concerning ImageMagick, and can be omitted on all other distros. The final three arguments are the two input files and the output file, which is the same as the second input file. Rinse and repeat for the small version too.

BASHing Through Scripts Further manipulation BASHing Through Scripts Further manipulation
Click to enlarge.

Now we’ve got ourselves a script that resizes and watermarks our images, but only works on single files. But wouldn’t it be a lot easier if it worked on entire directories? Of course it will be! And it’s in fact easy to do so. A ‘for’ loop is our weapon of choice now. But before we start coding, there’s one thing we have to do - and that’s move our watermark image. If it remains in the same directory as our image(s), it will be treated the same way - resized and watermarked. So for ease of work I moved it up a directory (“mv BASHWatermark.png ..”).

OK, now, let’s finish this…

 #! /bin/bash

for i in *
do
height=`identify -format %h ${i}`
width=`identify -format %w ${i}`

if [ ${width} -lt ${height} ]
then
convert -resize "275x365" ${i} small_${i}
else
convert -resize "365x275" ${i} small_${i}
fi

composite -gravity southwest -compose src-over ../BASHWatermark.png ${i} ${i}
composite -gravity southwest -compose src-over ../BASHWatermark.png small_${i} small_${i}
done

Click to enlarge.

Click to enlarge.

I made some minor changes. Firstly, I removed the workaround at the beginning (i="$1"), because I have other plans for the variable i. I placed the entire script in a ‘for’ loop, using i as a variable for all files found in the current directory (*). I also put ‘../’ in front of the watermark image files, to indicate that the file is a directory up. If you run the script without extra arguments, all of the files in the current directory (from which the script is run) are treated.

When you run the script you’ll get some errors about the image format of “imageconvert.sh”. This is because our loop does every file in the directory, which includes our script. Of course our script can’t be resized and watermarked. It's easy to fix this, but we won't get into that here.

This is where this guide stops, but it doesn’t mean that you have to. You can make the watermark image filename a variable which you can pass as an argument or set at the top, you can alter the output file, remove the error, …

The sky, and your imagination, is the limit.

Conclusion

I hope this crash course in shell scripting has shown you that although BASH is a shell, it’s more than capable of doing powerful things. Don’t get me wrong tough, BASH is small compared to PHP, perl or python - but it can stand its ground on usability and ease. The BASH script is just a small step up from a shell command, too - so you can even perform loops and the likes from your command line prompt!

If it sparked your attention, be sure to read some of the many beginner guides to BASH. Or if you are feeling brave, move on to some more advanced BASH scripting. Or, you can just wait right here for my next instalment!
Discuss this in the forums
YouTube logo
MSI MPG Velox 100R Chassis Review

October 14 2021 | 15:04

TOP STORIES

SUGGESTED FOR YOU